<?php
namespace app\controller\api;
// use app\BaseController;
use app\FrontendController;
use app\model\frontend\CategoryModel;
use app\model\frontend\SettingModel;
use app\model\tool\ImageModel;
use app\model\cms\ArticleModel;
class Home extends FrontendController{

    private $token=null;
    private $customer_bg_img_default = '/storage/assets/image/user-cover.png';
    public function index(){

        $category = new CategoryModel();
//        $cat = $category->get_list();
        $cat = $category->tree();

        echo json_encode( $cat );
    }

    // 关于我们页面， 专用： 熠远精科个人小程序
    public function contact(){

       $res['code'] = 0;
        
        $res['article'] = ArticleModel::where( 'title', '公司介绍' )->find( );

        if( empty( $res['article'] ) ){
            $res['code'] = 1;
            $res['meaasge'] = '文章不存在';
            return json( $res ); 
        }

        $res['article']->content = html_entity_decode( $res['article']->content );

        $res['article']->image = ImageModel::resize2( $res['article']->image, 350,350 );
        return json( $res );
        
    }

    public function setting(){
        $shop_name = SettingModel::get('config','shop_name');
        $home_live_show = SettingModel::get('config','home_live_show');
        $phone = SettingModel::get('config','phone');
        $float_nav_show = SettingModel::get('xcx','float_nav_show');
        $diy_id = SettingModel::getItem( 'diy', 'home' );
        $json = [
            'status'=>0,
            'settings'=>[
                'shop_name'     => !empty($shop_name)?$shop_name['value']:'',
                'phone'         => !empty($phone)?$phone['value']:'',
                'home_live_show'=> !empty($home_live_show)? intval( $home_live_show['value'] ):1,
                'float_nav_show'=> !empty($float_nav_show)? intval( $float_nav_show['value'] ):1,
                'diy_id'        => $diy_id,
            ]
        ];

        echo json_encode( $json );
    }

    public function diy(){

        $json = [
            'status'=>0,
        ];
        $json['diy_id'] = SettingModel::getItem( 'diy', 'home' );

        echo json_encode( $json );
    }

    public function init_setting(){
        
        global $_GPC;
        // global $_W;
        $res = [
            'status'=>0
        ];



        $app_setting = SettingModel::get_app();
        if( !empty( $app_setting['wx_app_id'] ) || !empty( $app_setting['wx_app_secret'] ) ){
            $appId = $app_setting['wx_app_id'];
//            $appSecret = $app_setting['wx_app_secret'];
        }

        $xcx_setting = SettingModel::get_xcx();
        $res['shop_name'] = !empty($xcx_setting['shop_name'])?$xcx_setting['shop_name']:'神秘商城';
        $res['location_key'] = !empty($xcx_setting['location_key'])?$xcx_setting['location_key']:'';

        $config_setting = SettingModel::get_config();
        $res['shop_phone']      = !empty($config_setting['phone'])          ? $config_setting['phone']          : '';
        $res['shop_wechat']     = !empty($config_setting['wechat'])         ? $config_setting['wechat']         : '';
        $res['shop_qq']         = !empty($config_setting['qq'])             ? $config_setting['qq']             : '';
        $res['float_nav_show']  = !empty($xcx_setting['float_nav_show'])    ? $xcx_setting['float_nav_show']    : 0;

        // todo 获取用户中心背景图片
        $customer_bg_img = SettingModel::get('customer_center','bg_img');
        // $this->customer_bg_img_default = $this->url_host . $this->customer_bg_img_default;
        $this->customer_bg_img_default = '';
        // $res['customer_bg_img'] = $customer_bg_img ? tomedia($customer_bg_img['value']): $this->customer_bg_img_default;
        $res['customer_bg_img'] = $customer_bg_img ? ImageModel::resize2($customer_bg_img['value']): $this->customer_bg_img_default;
        $customer_enter_box = SettingModel::get('customer_center','entry_box');
        $res['customer_entry_box'] = $customer_enter_box?$customer_enter_box['value']:0;
        // $res['app_id'] = $appId;
        $res['app_id'] = !empty($appId) ? $appId : (!empty($xcx_setting['app_id'])?$xcx_setting['app_id']:'');

        echo json_encode( $res );
    }

}
